﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;

namespace HZ.Supervise.Web.Secondphase.Statistics
{
    public partial class StatisticsHospitalBySeason : BasePage
    {
        DataTable dt = new DataTable();
        DataTable dtpie = new DataTable();
        public string Hid;
        string year;
        string season;
        string StartTimeFrom = "";
        string StartTimeTo = "";
        string hname;
        public StatisticsHospitalBySeason()
        {
            this.ModuleNo = Module.报表统计.ToString();
            this.PageOperateType = OperateType.医院季报.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Hid = Request["Y"];
			if (!string.IsNullOrEmpty(Hid))
			{
                hname = HttpUtility.UrlDecode(Request["Y"].ToString());
			}
            if (!IsPostBack)
            {
                year = DateTime.Now.Year.ToString();
                //判断dropdwonList中的年份是当前的年份
                foreach (ListItem item in this.droYear.Items)     //2011.4.12 by 张雄 modify
                {
                    //判断是否当前年份如果是则显示当前年份
                    if (item.Text == year.ToString())
                    {
                        item.Selected = true;
                        break;
                    }
                }
                

                GetSeason(DateTime.Now.Month.ToString());
                droSeason.Text = (int.Parse(season) - 1).ToString();
                dataBind();
                BindMSChat();
                GetStatistics();
            }
        }

        private void GetSeason(string i)
        {
            switch (i)
            {
                case "1":
                    season = "1";

                    break;
                case "2":
                    season = "1";

                    break;
                case "3":
                    season = "1";

                    break;
                case "4":
                    season = "2";

                    break;
                case "5":
                    season = "2";

                    break;

                case "6":
                    season = "2";

                    break;
                case "7":
                    season = "3";

                    break;
                case "8":
                    season = "3";

                    break;
                case "9":
                    season = "3";

                    break;
                case "10":
                    season = "4";
                    break;
                case "11":
                    season = "4";
                    break;
                case "12":
                    season = "4";
                    break;
            }


        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        private void dataBind()
        {
            HZ.Supervise.IBLL.IExecuteContext ie = HZ. Supervise.BLLFactory.ExecuteContext();

            string Year = string.Intern(this.droYear.SelectedItem.Text);

            string Season = string.Intern(this.droSeason.SelectedItem.Text);

            dt = ie.StatisticsHospitalBySeason(hname, Year, Season);

            HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate");

            HZ.Supervise.Web.Function.ChangeRate(dt, "DistributeRate", "AddDistributeRate");

			DataTable dtGV = new DataTable();

			DataView dv = dt.DefaultView;

			dv.Sort = " daytime desc";

			dtGV = dv.ToTable();

            gvwStatisticsHos.DataSource = dtGV;

            gvwStatisticsHos.DataBind();

            HZ.Supervise.Web.Function.Combine(gvwStatisticsHos);


            //饼状图 
            dtpie = ie.StatisticsHospitalBySeasonPie(hname, Year, Season);

            HZ.Supervise.Web.Function.ChangeRate(dtpie, "WareHouseRate", "AddWareHouseRate");

            HZ.Supervise.Web.Function.ChangeRate(dtpie, "DistributeRate", "AddDistributeRate");

            gvwStatisticsHos1.DataSource = dtpie;

            gvwStatisticsHos1.DataBind();

            HZ.Supervise.Web.Function.Combine(gvwStatisticsHos1);

        }
        /// <summary>
        /// 合并单元格
        /// 2011.4.10 张雄 create
        /// </summary>
        private void SpanCell()
        {
            for (int j = 0; j < gvwStatisticsHos.Rows.Count; j++)
            {
                if (j < gvwStatisticsHos.Rows.Count)
                {
                    //第一行与第二行相等
                    if ((gvwStatisticsHos.Rows.Count - j) >= 2 && gvwStatisticsHos.Rows[j].Cells[0].Text == gvwStatisticsHos.Rows[j + 1].Cells[0].Text)
                    {
                        //第一行与第三行相等
                        if ((gvwStatisticsHos.Rows.Count - j) >= 3 && gvwStatisticsHos.Rows[j].Cells[0].Text == gvwStatisticsHos.Rows[j + 2].Cells[0].Text)
                        {
                            //第一行与第四行相等
                            if ((gvwStatisticsHos.Rows.Count - j) >= 4 && gvwStatisticsHos.Rows[j].Cells[0].Text == gvwStatisticsHos.Rows[j + 3].Cells[0].Text)
                            {
                                //第一行与第五行相等
                                if ((gvwStatisticsHos.Rows.Count - j) > 5 && gvwStatisticsHos.Rows[j].Cells[0].Text == gvwStatisticsHos.Rows[j + 4].Cells[0].Text)
                                {
                                    //头合并
                                    gvwStatisticsHos.Rows[j].Cells[0].RowSpan = 5;
                                    gvwStatisticsHos.Rows[j + 1].Cells[0].Visible = false;
                                    gvwStatisticsHos.Rows[j + 2].Cells[0].Visible = false;
                                    gvwStatisticsHos.Rows[j + 3].Cells[0].Visible = false;
                                    gvwStatisticsHos.Rows[j + 4].Cells[0].Visible = false;
                                    j += 4;
                                }
                                else
                                {
                                    //头合并
                                    gvwStatisticsHos.Rows[j].Cells[0].RowSpan = 4;
                                    gvwStatisticsHos.Rows[j + 1].Cells[0].Visible = false;
                                    gvwStatisticsHos.Rows[j + 2].Cells[0].Visible = false;
                                    gvwStatisticsHos.Rows[j + 3].Cells[0].Visible = false;
                                    j += 3;
                                }
                            }
                            else
                            {
                                //头合并
                                gvwStatisticsHos.Rows[j].Cells[0].RowSpan = 3;
                                gvwStatisticsHos.Rows[j + 1].Cells[0].Visible = false;
                                gvwStatisticsHos.Rows[j + 2].Cells[0].Visible = false;
                                j += 2;
                            }
                        }
                        else
                        {
                            //头合并
                            gvwStatisticsHos.Rows[j].Cells[0].RowSpan = 2;
                            gvwStatisticsHos.Rows[j + 1].Cells[0].Visible = false;
                            j += 1;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 绑定统计
        /// </summary>
        void GetStatistics()
        {
            double purchaseAmountTotal = 0;//采购总金额
            double distributionAmountTotal = 0;
            double wareHouseAmountTotal = 0;//入库总金额
            for (int i = 0; i < gvwStatisticsHos.Rows.Count; i++)
            {
                purchaseAmountTotal += double.Parse(gvwStatisticsHos.Rows[i].Cells[1].Text);
                distributionAmountTotal += double.Parse(gvwStatisticsHos.Rows[i].Cells[2].Text);
                wareHouseAmountTotal += double.Parse(gvwStatisticsHos.Rows[i].Cells[4].Text);
            }
            this.lblCount.Text = "采购总金额为:" + purchaseAmountTotal.ToString() + ";配送总金额为:" + distributionAmountTotal.ToString() + ";入库总金额为:" + wareHouseAmountTotal.ToString();

        }


        /// <summary>
        /// 曲线图绑定
        /// 2011.4.9 张雄 create
        /// </summary>
        private void BindMSChat()
        {
            //空为初始页
            if (dt.Rows.Count > 0)
            {

                this.divshow.Visible = true;

                string monch = ""; //月份
                #region 左图
                //***********************2010基本药品目录金额******************/
                Series series3 = new Series("采购金额");
                series3.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                //series3.MarkerStyle = MarkerStyle.Diamond;
                series3.BorderWidth = 1;
                series3.ShadowOffset = 1;
                series3.Color = Color.FromArgb(189, 68, 104);
                series3.Label = "#VAL{F}";
                series3.ToolTip = "采购金额: #VAL{c} 万元";

                //***************** 2010现价品种*****************/
                Series series4 = new Series("配送金额");
                series4.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                //series4.MarkerStyle = MarkerStyle.Diamond;
                series4.BorderWidth = 1;
                series4.ShadowOffset = 1;
                series4.Color = Color.FromArgb(100, 107, 253);
                series4.Label = "#VAL{F}";
                series4.ToolTip = "配送金额：#VAL{c} 万元";

                //***************** 2010现价品种*****************/
                Series series5 = new Series("入库金额");
                series5.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                //series5.MarkerStyle = MarkerStyle.Diamond;
                series5.BorderWidth = 1;
                series5.ShadowOffset = 1;
                series5.Color = Color.FromArgb(234, 193, 111);
                series5.Label = "#VAL{F}";
                series5.ToolTip = "入库金额：#VAL{c} 万元";
                #endregion

                #region 右图
                Series series1 = new Series("药品全年统计");
                series1.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Pie", true);
                series1.ToolTip = "#VALX: #VAL{c} 元";
                series1.Label = "#PERCENT{P}";
                series1.ShadowOffset = 2;
                series1.BorderColor = Color.DarkGray;
                //series1["CollectedColor"] = "red";
                #endregion

                #region 柱状图数据绑定
                int s = 0;      //用于记数

                string str = "";        //存储月份

                foreach (DataRow row in dt.Rows)
                {
                    monch = HZ.Supervise.Web.Function.FormartDate(row["daytime"].ToString());       //记到时间中的月份

                    if (!str.Contains(monch))
                    {
                        str += monch + ",";     //月份加入字符串

                        series3.Points.AddXY(monch, Convert.ToDouble(row["PurchaseAmountTotal"].ToString()) / 10000);
                        //series3.Points[s].ToolTip = (Convert.ToDouble(row["PurchaseAmountTotal"].ToString()) / 10000).ToString();

                        series4.Points.AddXY(monch, Convert.ToDouble(row["DistributeAmountTotal"].ToString()) / 10000);
                        //series4.Points[s].ToolTip = (Convert.ToDouble(row["DistributeAmountTotal"].ToString()) / 10000).ToString();

                        series5.Points.AddXY(monch, Convert.ToDouble(row["WarehouseAmountTotal"].ToString()) / 10000);
                        //series5.Points[s].ToolTip = row["WarehouseAmountTotal"].ToString();

                        s++;
                    }
                }

                #endregion

                #region 饼图绑定到数据
                IList<HZ.Supervise.Model.ChartModel> chartlist = HZ.Supervise.DAL.DAL_Helper.CommonFillList<HZ.Supervise.Model.ChartModel>(dtpie);

                series1.Points.DataBind(chartlist, "SortSource", "PurchaseAmountTotal", "LegendText=SortSource,YValues=PurchaseAmountTotal");

				//曹鹏 2011年5月23日13:19:41 判断采购类型
				for (int i = 0; i < chartlist.Count; i++)
				{

                    if (chartlist[i].SortSource == "日常采购")
					{

						series1.Points[i].Color = Color.FromArgb(74, 172, 197);

					}
                    else if (chartlist[i].SortSource == "临时采购")
					{

						series1.Points[i].Color = Color.FromArgb(129, 101, 162);

					}
                    else if (chartlist[i].SortSource == "应急采购")
					{

						series1.Points[i].Color = Color.FromArgb(192, 80, 78);

					}

				}
				//series1.Points[0].Color = Color.FromArgb(74, 172, 197);
				//series1.Points[1].Color = Color.FromArgb(129, 101, 162);
				//series1.Points[2].Color = Color.FromArgb(192, 80, 78);
                #endregion

                #region 绑定到图
                series3.IsValueShownAsLabel = true;
                ChartPurchasing.Series.Add(series3);

                series4.IsValueShownAsLabel = true;
                ChartPurchasing.Series.Add(series4);

                series5.IsValueShownAsLabel = true;
                ChartPurchasing.Series.Add(series5);

                series1.IsValueShownAsLabel = true;
                ChartInStorage.Series.Add(series1);
                #endregion

                #region 两张图片属性

                ChartPurchasing.ChartAreas["ChartHosPurchasing"].AxisX.IsMarginVisible = true;
                ChartPurchasing.ChartAreas["ChartHosPurchasing"].Area3DStyle.Enable3D = false;

                ChartPurchasing.ChartAreas["ChartHosPurchasing"].AxisX.MajorGrid.Interval = 1;
                ChartPurchasing.ChartAreas["ChartHosPurchasing"].AxisX.LineWidth = 1;
                ChartPurchasing.ChartAreas["ChartHosPurchasing"].AxisY.LineWidth = 1;

                ChartPurchasing.ChartAreas["ChartHosPurchasing"].AxisY.Title = "金额（万元）";
                ChartPurchasing.ChartAreas["ChartHosPurchasing"].AxisX.Title = "日期";
                ChartPurchasing.ChartAreas["ChartHosPurchasing"].Position.Auto = true;


                #endregion
                lblnull.Visible = false;
            }
            else
            {
                //没有数据则显示“没数据”
                this.divshow.Visible = false;
                this.ChartPurchasing.Visible = false;
                this.lblnull.Visible = true;

                this.divTable.Visible = false;
                this.btnExcel1.Visible = false;
            }
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            year = this.droYear.SelectedValue;
            if (droSeason.SelectedValue != "-1")
            {
                season = this.droSeason.SelectedValue;
            }
            //GetTime(season);
            dataBind();
            BindMSChat();
            GetStatistics();
        }

        /// <summary>
        /// 显示模式转换
        /// by张雄 create
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void dropmode_SelectedIndexChanged(object sender, EventArgs e)
        {
            year = this.droYear.SelectedValue;

            string Season = this.droSeason.SelectedValue;
            GetSeason(Season);

            dataBind();
            BindMSChat();
            GetStatistics();
        }

        /// <summary>
        /// 导出柱状图数据
        /// by张雄  2011.5.20
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void btnExcel1_Click(object sender, EventArgs e)
        {

            HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();

            string Year = string.Intern(this.droYear.SelectedItem.Text);

            string Season = string.Intern(this.droSeason.SelectedItem.Text);

            dt = ie.StatisticsHospitalBySeason(hname, Year, Season);

            HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate");

            HZ.Supervise.Web.Function.ChangeRate(dt, "DistributeRate", "AddDistributeRate");

			DataTable dtGV = new DataTable();

			DataView dv = dt.DefaultView;

			dv.Sort = " daytime desc";

			dtGV = dv.ToTable();

            HZ.Excel.ExcelHelper.ExportDataForWeb(dtGV, this.gvwStatisticsHos);
        }

        /// <summary>
        /// 导出饼图数据
        /// by张雄  2011.5.20
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void btnExcel2_Click(object sender, EventArgs e)
        {

            HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();

            string Year = string.Intern(this.droYear.SelectedItem.Text);

            string Season = string.Intern(this.droSeason.SelectedItem.Text);

            dtpie = ie.StatisticsHospitalBySeasonPie(hname, Year, Season);

            HZ.Supervise.Web.Function.ChangeRate(dtpie, "WareHouseRate", "AddWareHouseRate");

            HZ.Supervise.Web.Function.ChangeRate(dtpie, "DistributeRate", "AddDistributeRate");

            HZ.Excel.ExcelHelper.ExportDataForWeb(dtpie, this.gvwStatisticsHos1);
        }

        /// <summary>
        /// 医院表头
        /// by张雄  2011.5.24
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwStatisticsHos_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:

                    TableCellCollection tcHeader = e.Row.Cells;
                    tcHeader.Clear();

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[0].Attributes.Add("colspan", "8"); //跨Column
                    tcHeader[0].Attributes.Add("bgcolor", "LightSteelBlue");
                    SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                    tcHeader[0].Text = (hname == null ? "医院汇总统计" : hname) + "</th></tr><tr>";

                    //第2行表头
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[1].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[1].Text = "日期";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[2].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[2].Text = "采购总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[3].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[3].Text = "配送总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[4].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[4].Text = "配送率";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[5].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[5].Text = "入库金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[6].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[6].Text = "入库率";

                    break;
            }
        }

        protected void gvwStatisticsHos1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:

                    TableCellCollection tcHeader = e.Row.Cells;
                    tcHeader.Clear();

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[0].Attributes.Add("colspan", "8"); //跨Column
                    tcHeader[0].Attributes.Add("bgcolor", "LightSteelBlue");
                    SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                    tcHeader[0].Text = (hname == null ? "医院汇总统计" : hname) + "</th></tr><tr>";

                    //第2行表头
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[1].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[1].Text = "采购类型";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[2].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[2].Text = "采购总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[3].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[3].Text = "配送总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[4].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[4].Text = "配送率";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[5].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[5].Text = "入库金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[6].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[6].Text = "入库率";

                    break;
            }
        }
    }
}
